package com.spdzen.controller.admin;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.model.SysRole;
import com.spdzen.service.SysRoleService;
import com.spdzen.support.JsonUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.JsonResult;
import com.spdzen.support.validation.AddValidGroup;
import com.spdzen.support.validation.UpdateValidGroup;

@Controller
@RequestMapping("/admin/sysrole")
public class SysRoleAdminController {
	@Resource
	private SysRoleService sysRoleService;

	@RequestMapping("/pageView.do")
	public String pageView() {
		return "/admin/sysrole/sysRolePageView";
	}

	@RequestMapping("/queryPage.do")
	@ResponseBody
	public JsonResult queryPage(PageOffsetParam param) {
		try {
			return JsonResult.success(sysRoleService.selectPage(param));
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(param), e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/queryAll.do")
	@ResponseBody
	public JsonResult queryAll() {
		try {
			return JsonResult.success(sysRoleService.selectAll());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}

	@RequestMapping("/add.do")
	@ResponseBody
	public JsonResult add(@Validated(value = { AddValidGroup.class }) SysRole entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}
		
		JsonResult jr = null;
		try {
			sysRoleService.inserIfPermission(entity);
			jr = JsonResult.success();
		} catch (ResultCodeException e) {
			jr = new JsonResult(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			jr = JsonResult.error();
		}
		
		return jr;
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonResult update(@Validated(value = { UpdateValidGroup.class }) SysRole entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}
		
		JsonResult jr = null;
		try {
			sysRoleService.updateIfPermission(entity);
			jr = JsonResult.success();
		} catch (ResultCodeException e) {
			jr = new JsonResult(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			jr = JsonResult.error();
		}
		
		return jr;
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonResult delete(Long id) {
		if (id == null || id.longValue() < 1) {
			return JsonResult.errorParameter();
		}
		
		JsonResult jr = null;
		try {
			sysRoleService.deleteIfPermission(id);
			jr = JsonResult.success();
		} catch (ResultCodeException e) {
			jr = new JsonResult(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			jr = JsonResult.error();
		}
		
		return jr;
	}
}
